package com.appkubes.mall.restful.application.pay.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.common.constants.BaseConstants;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.restful.application.order.order.service.OrderService;
import com.appkubes.mall.restful.application.pay.entity.dto.PayOrderDTO;
import com.appkubes.mall.restful.application.pay.entity.vo.PayOrderVO;
import com.appkubes.mall.restful.application.pay.service.PayService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Rongjie
 * @Date: 2020/2/28
 * @Description:
 */
@Api(tags = "支付中心模块")
@RestController
@RequestMapping("/pay")
public class PayOrderController {

    @Resource
    OrderService orderService;
    @Resource
    List<PayService> payServices; //支持多种支付方式, 工厂方法解偶

    //订单支付
    @PostMapping("order/{orderKey}")
    public Response payOrder(@PathVariable("orderKey") String orderKey,
                             @ParamValid PayOrderVO payOrderVO) {
        MallOrder order = orderService.getById(orderKey);

        if (!BaseConstants.ORDER.UNPAY.equals(order.getStatus())) {
            return Response.error("订单已过期");
        }

        PayOrderDTO resultDTO = new PayOrderDTO();
        for (PayService payService : payServices) {
            //使用支付路由决定使用的支付实现方法
            if (payOrderVO.getPaymentType().equalsIgnoreCase(payService.getPaymentType().name())) {
                //确定该支付方法
                resultDTO = payService.pay(order.getOrderKey());
                break;
            }
        }
        return Response.success(resultDTO);
    }
}
